/*
 * metismenu - v1.1.3
 * Easy menu jQuery plugin for Twitter Bootstrap 3
 * https://github.com/onokumus/metisMenu
 *
 * Made by Osman Nuri Okumus
 * Under MIT License
 */
;(function($, window, document, undefined) {

    getMenu();
    function  getMenu() {
        $.ajax({
            url : '/menu/tree',
            type : 'GET',
            dataType : 'json',
            async:false,
            success : function(data) {
                menuInit(data.data)
            }
        });
    }


    function menuInit(menuArr){
        let str=''
        for (let i = 0; i < menuArr.length; i++) {
            let obj=menuArr[i];
            if(obj.child===undefined || obj.child.length===0){
                str+='<li>';
                if(obj.istarget===0){
                    str+='		<a href="'+obj.href+'" class="J_menuItem">';
                }else {
                    str+='		<a href="'+obj.href+'"  target="_blank">';
                }
                str+=' 			<i class="'+obj.icon+'"></i>'
                str+='          <span class="nav-label">'+obj.menuname+'</span>'
                str+='		</a>';
                str+='</li>';
            }else {
                str+='<li>'
                str+='		<a href="#">'
                str+='			<i class="'+obj.icon+'"></i>'
                str+='			<span class="nav-label">'+obj.menuname+'</span>'
                str+='			<span class="fa arrow"></span>'
                str+='		</a>'
                str+='		<ul class="nav nav-second-level">'
                str+= childTree(obj.child)
                str+='      </ul>'
                str+='</li>'
            }
        }
        let html=document.getElementById('side-menu').innerHTML;
        str=html+str;
        document.getElementById('side-menu').innerHTML=str;
    }

    function childTree(childlist) {
        let str1='';
        for (let i = 0; i < childlist.length; i++) {
            let obj=childlist[i];
            if(obj.child===undefined || obj.child.length===0){
                str1+='<li>';
                if(obj.istarget===0){
                    str1+='		<a href="'+obj.href+'" class="J_menuItem">';
                }else {
                    str1+='		<a href="'+obj.href+'"  target="_blank" >';
                }
                str1+=' 		<i class="'+obj.icon+'"></i>'
                str1+='          <span class="nav-label">'+obj.menuname+'</span>'
                str1+='		</a>';
                str1+='</li>';
            }else {
                str1+='<li>'
                str1+='		<a href="#">'
                str1+='			<i class="'+obj.icon+'"></i>'
                str1+='			<span class="nav-label">'+obj.menuname+'</span>'
                str1+='			<span class="fa arrow"></span>'
                str1+='		</a>'
                str1+='		<ul class="nav nav-second-level">'
                str1+=childTree(obj.child)
                str1+='		</ul>'
                str1+='</li>'
            }
        }
        return str1
    }

    var pluginName = "metisMenu",
        defaults = {
            toggle: true,
            doubleTapToGo: false
        };

    function Plugin(element, options) {
        this.element = $(element);
        this.settings = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }

    Plugin.prototype = {
        init: function() {

            var $this = this.element,
                $toggle = this.settings.toggle,
                obj = this;

            if (this.isIE() <= 9) {
                $this.find("li.active").has("ul").children("ul").collapse("show");
                $this.find("li").not(".active").has("ul").children("ul").collapse("hide");
            } else {
                $this.find("li.active").has("ul").children("ul").addClass("collapse in");
                $this.find("li").not(".active").has("ul").children("ul").addClass("collapse");
            }

            //add the "doubleTapToGo" class to active items if needed
            if (obj.settings.doubleTapToGo) {
                $this.find("li.active").has("ul").children("a").addClass("doubleTapToGo");
            }

            $this.find("li").has("ul").children("a").on("click" + "." + pluginName, function(e) {
                e.preventDefault();

                //Do we need to enable the double tap
                if (obj.settings.doubleTapToGo) {

                    //if we hit a second time on the link and the href is valid, navigate to that url
                    if (obj.doubleTapToGo($(this)) && $(this).attr("href") !== "#" && $(this).attr("href") !== "") {
                        e.stopPropagation();
                        document.location = $(this).attr("href");
                        return;
                    }
                }

                $(this).parent("li").toggleClass("active").children("ul").collapse("toggle");

                if ($toggle) {
                    $(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
                }

            });
        },

        isIE: function() { //https://gist.github.com/padolsey/527683
            var undef,
                v = 3,
                div = document.createElement("div"),
                all = div.getElementsByTagName("i");

            while (
                div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->",
                    all[0]
                ) {
                return v > 4 ? v : undef;
            }
        },

        //Enable the link on the second click.
        doubleTapToGo: function(elem) {
            var $this = this.element;

            //if the class "doubleTapToGo" exists, remove it and return
            if (elem.hasClass("doubleTapToGo")) {
                elem.removeClass("doubleTapToGo");
                return true;
            }

            //does not exists, add a new class and return false
            if (elem.parent().children("ul").length) {
                //first remove all other class
                $this.find(".doubleTapToGo").removeClass("doubleTapToGo");
                //add the class on the current element
                elem.addClass("doubleTapToGo");
                return false;
            }
        },

        remove: function() {
            this.element.off("." + pluginName);
            this.element.removeData(pluginName);
        }

    };

    $.fn[pluginName] = function(options) {
        this.each(function () {
            var el = $(this);
            if (el.data(pluginName)) {
                el.data(pluginName).remove();
            }
            el.data(pluginName, new Plugin(this, options));
        });
        return this;
    };

})(jQuery, window, document);
